app.controller("goodsCategoryController",function ($scope,goodsCategoryService) {
    //$controller("commonController",{$scope:$scope});
    //记录上级 id
    $scope.parentId=0;

    //通过父 id 查询分类
    $scope.findByParentId=function (id) {
        $scope.parentId=id;//记住上级ID
        goodsCategoryService.findByParentId(id).success(
          function (response) {
            $scope.list=response;
          }
        );
    }

    //查找详情
    $scope.findOne=function(id){
        goodsCategoryService.findOne(id).success(
            function (response) {
                $scope.entity=response;
            }
        );
    }

    //设置 3 级面包屑
    $scope.grade=1;
    $scope.setGrade=function (value) {
        $scope.grade=value;
    }

//读取列表
    $scope.selectList=function(p_entity){
        if($scope.grade==1){//如果为1级
            $scope.entity_1=null;
            $scope.entity_2=null;
        }
        if($scope.grade==2){//如果为2级
            $scope.entity_1=p_entity;
            $scope.entity_2=null;
        }
        if($scope.grade==3){//如果为3级
            $scope.entity_2=p_entity;
        }
        $scope.findByParentId(p_entity.id);
    }

    //保存
    $scope.save=function(){
        var serviceObject;//服务层对象
        if($scope.entity.id!=null){//如果有ID
            serviceObject=goodsCategoryService.update( $scope.entity ); //修改
        }else{
            $scope.entity.parentId=$scope.parentId;//赋予上级ID
            serviceObject=goodsCategoryService.add( $scope.entity  );//增加
        }
        serviceObject.success(
            function(response) {
                if (200==response.code) {
                    //重新查询
                    $scope.findByParentId($scope.parentId);
                } else {
                    alert(response.message);
                }
            });
    }

    //选中的要删除的
    $scope.selected = [];
    $scope.updateSelected = function ($event, id) {
        if ($event.target.checked) {
            $scope.selected.push(id);
        } else {
            var idx = $scope.selected.indexOf(id);
            $scope.selected.splice(idx, 1);
        }
    }

    //删除
    $scope.delete=function () {
        goodsCategoryService.delete($scope.selected).success(
            function (response) {
                if (200==response.code) {
                    //重新查询
                    $scope.findByParentId($scope.parentId);
                    $scope.selected = [];
                } else {
                    alert(response.message);
                }
        });
    }

});